import init, {
	app_init,

	get_difficult_mode,
	set_difficult_mode,

	get_theme,
	set_theme,

	get_symbol_mode,
	set_symbol_mode,

	new_game,

	press_enter,
	press_backspace,
	press_char
} from "./pkg/wordle.js"

// difficult mode interface
let difficultMode = false
function setDifficultModeButton(flag) {
	if (difficultMode = flag)
		DIFFICULT_MODE_BUTTON.innerText = '困难模式'
	else
		DIFFICULT_MODE_BUTTON.innerText = '普通模式'
}
function toggleDifficultModeButton() {
	setDifficultModeButton(!difficultMode)
	set_difficult_mode(difficultMode)
}

// theme interface
let theme = 'dark'
function setThemeButton(newTheme) {
	theme = newTheme
	THEME_STYLESHEET.setAttribute('href', 'assets/' + theme + '.css')
	if (theme == 'dark')
		THEME_BUTTON.innerText = '☽深色主题'
	else
		THEME_BUTTON.innerText = '☼浅色主题'
}
function toggleThemeButton() {
	setThemeButton(theme == 'dark' ? 'light' : 'dark')
	set_theme(theme)
}

// symbol mode interface
let symbolMode = false
function setSymbolModeButton(flag) {
	if (symbolMode = flag)
		SYMBOL_MODE_STYLESHEET.setAttribute('href', 'assets/symbol.css')
	else
		SYMBOL_MODE_STYLESHEET.setAttribute('href', 'assets/none.css')
	if (symbolMode)
		SYMBOL_MODE_BUTTON.innerText = '!?符号模式：开'
	else
		SYMBOL_MODE_BUTTON.innerText = '×符号模式：关'
}
function toggleSymbolModeButton() {
	setSymbolModeButton(!symbolMode)
	set_symbol_mode(symbolMode)
}

// set onload function
window.onload = function () {
	init().then(function () {
		// init application state
		app_init()

		// get difficult mode
		setDifficultModeButton(get_difficult_mode())
		// create difficult mode toggler
		DIFFICULT_MODE_BUTTON.addEventListener('click', toggleDifficultModeButton)

		// get theme
		setThemeButton(get_theme())
		// create theme toggler
		THEME_BUTTON.addEventListener('click', toggleThemeButton)

		// get symbol mode
		setSymbolModeButton(get_symbol_mode())
		// create symbol mode toggler
		SYMBOL_MODE_BUTTON.addEventListener('click', toggleSymbolModeButton)

		// start a new game
		new_game()
	})
}

// restart interface
RESTART_BUTTON.addEventListener('click', function () {
	new_game()
})

// reset statistics
RESET_STAT_BUTTON.addEventListener('click', function () {
	if (confirm('⚠进行此操作后，您将丢失所有统计数据。是否仍然继续？')) {
		document.cookie = 'state=0;max-age=0'
		location.reload()
	}
})

// reset statistics
RESET_CONFIG_BUTTON.addEventListener('click', function () {
	if (confirm('⚠进行此操作后，您将丢失所有设置。是否仍然继续？')) {
		document.cookie = 'config=0;max-age=0'
		location.reload()
	}
})

// key interface
enterButton.addEventListener('click', press_enter)
backspaceButton.addEventListener('click', press_backspace)
for (let i = 0; i < ALPHABET_SIZE; ++i)
	letterButtonArray[i].addEventListener('click', function () { press_char(i) })

document.addEventListener('keydown', function (event) {
	switch (event.code) {
		case 'Enter': press_enter(); break
		case 'Backspace': press_backspace(); break

		case 'KeyA': press_char(0); break
		case 'KeyB': press_char(1); break
		case 'KeyC': press_char(2); break
		case 'KeyD': press_char(3); break
		case 'KeyE': press_char(4); break
		case 'KeyF': press_char(5); break
		case 'KeyG': press_char(6); break

		case 'KeyH': press_char(7); break
		case 'KeyI': press_char(8); break
		case 'KeyJ': press_char(9); break
		case 'KeyK': press_char(10); break
		case 'KeyL': press_char(11); break
		case 'KeyM': press_char(12); break
		case 'KeyN': press_char(13); break

		case 'KeyO': press_char(14); break
		case 'KeyP': press_char(15); break
		case 'KeyQ': press_char(16); break
		case 'KeyR': press_char(17); break
		case 'KeyS': press_char(18); break
		case 'KeyT': press_char(19); break

		case 'KeyU': press_char(20); break
		case 'KeyV': press_char(21); break
		case 'KeyW': press_char(22); break
		case 'KeyX': press_char(23); break
		case 'KeyY': press_char(24); break
		case 'KeyZ': press_char(25); break
	}
})
